package com.nintendo.controller;

import com.nintendo.bean.ChatEntity;
import com.nintendo.bean.ChatRecord;
import com.nintendo.service.ChatRecordService;
import com.nintendo.service.OllamaService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ollama")
@Slf4j
@CrossOrigin
public class OllamaController {

    @Resource
    private OllamaService ollamaService;
    @Resource
    private ChatRecordService chatRecordService;
    @GetMapping("/ai/chat")
    public Object aiOllamaChat(@RequestParam String msg){
        return ollamaService.aiOllamaChat(msg);
    }
    @GetMapping("/ai/stream1")
    public Flux<ChatResponse>  aiOllamaChatStream1(@RequestParam String msg){
        Prompt prompt = new Prompt(new UserMessage(msg));
        Flux<ChatResponse> streamResponse = ollamaService.stream(prompt);
        return streamResponse;
    }
    @GetMapping("/ai/stream2")
    public List<String>  aiOllamaChatStream2(@RequestParam String msg){
        return ollamaService.stream2(msg);
    }
    @PostMapping("/ai/doctor/strem")
    public void aiOllamaChatStreamV3Doctor(@RequestBody ChatEntity chatEntity){
        log.info("请求参数：{}", chatEntity.toString());
        String userName = chatEntity.getCurrentUserName();
        String message = chatEntity.getMessage();
        ollamaService.doDoctorV3(userName,message);
    }
    @GetMapping("/getRecords")
    public List<ChatRecord> getRecords(@RequestParam("who")  String who){
        return chatRecordService.getChatRecord(who);
    }
}
